package de.blitzcoder.collide.gui.textarea;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import org.gjt.sp.jedit.IPropertyManager;
import org.gjt.sp.jedit.syntax.SyntaxStyle;
import org.gjt.sp.util.IOUtilities;
import org.gjt.sp.util.SyntaxUtilities;

/**
 *
 * @author blitzcoder
 */
public class CollIDETextAreaPropertyManager implements IPropertyManager {

    private Properties props;

    private static final String FILE = "textarea.props";
    
    public CollIDETextAreaPropertyManager() {
        props = new Properties();
        FileInputStream in=null;
        try {
            in = new FileInputStream(FILE);
            props.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtilities.closeQuietly(in);
        }
    }

    // Create a backup
    public Object getBackup() {
        return props.clone();
    }
    
    public void restoreBackup(Object backup) {
        if (backup instanceof Properties) {
            this.props = (Properties) backup;
            return;
        }
        throw new IllegalArgumentException("backup has to be Properties");
    }

    public void save() {
        try {
            FileOutputStream stream = new FileOutputStream(new File(FILE));
            props.store(stream, "CollIDE Textarea Settings");
            stream.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    @Override
    public String getProperty(String name) {
        return props.getProperty(name);
    }
    
    public int getIntProperty(String name) {
        return Integer.parseInt(props.getProperty(name));
    }
    
    public SyntaxStyle getSyntaxStyleProperty(String name) {
        return SyntaxUtilities.parseStyle(props.getProperty(name),"Arial",16,true);
    }
    
    public void setProperty(String key,String value) {
        props.setProperty(key, value);
    }
    
    public void setSyntaxStyleProperty(String name,SyntaxStyle style) {
        setProperty(name, buildStyleString(style));
    }
    
    public boolean getBooleanProperty(String name) {
        return Boolean.parseBoolean(props.getProperty(name));
    }

    public void setBooleanProperty(String name,boolean v) {
        setProperty(name, ""+v);
    }

    private String buildStyleString(SyntaxStyle style) {
        
        Color color = style.getForegroundColor();
        Color bgColor = style.getBackgroundColor();
        
        // Sometimes bgColor is empty. This is a bug
        // TODO: fix this
        if (bgColor == null)
            bgColor = Color.WHITE;
        
        String str = "color:"+SyntaxUtilities.getColorHexString(color)+" bgColor:"+SyntaxUtilities.getColorHexString(bgColor);
        if (style.getFont().isBold() || style.getFont().isItalic()) {
            str+=" style:";
            if (style.getFont().isBold())
                str+="b";
            if (style.getFont().isItalic())
                str+="i";
        }
        return str;
    }
    
    public void printValues() {
        props.list(System.out);
    }
    
}
